<template>
	<view class="bg">
		<view class="form">
			<view class="label">姓名:</view>
			<view class="value" :style="{'border-bottom':nameSelect?'1px solid #408CFF':'1px solid #e6e6e6'}">
				<input class="uni-input" name="input" placeholder="请输入姓名" :value="name" @input="getUserName" maxlength="10" @focus="changeBorder('nameSelect')"/>
			</view>
			<view class="label">手机号:</view>
			<view class="value" :style="{'border-bottom':mobileSelect?'1px solid #408CFF':'1px solid #e6e6e6'}">
				<input class="uni-input" name="input" placeholder="请输入手机号" :value="mobile" @input="getUserMobile" type="number" @focus="changeBorder('mobileSelect')"/>
			</view>
			<view class="label">验证码</view>
			<view class="code" :style="{'border-bottom':codeSelect?'1px solid #408CFF':'1px solid #e6e6e6'}">
				<input class="uni-input" name="input" placeholder="请输入验证码" :value="code" @input="getCode" maxlength="4" @focus="changeBorder('codeSelect')"/>
				<image :src="url" @click="getCodeImg" style="width: 25%;" mode="widthFix"></image>
			</view>
		</view>
		<view class="bottom">
			<button class="btn" @click="onSubmit">确定</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				mobile: '',
				code: '',
				nameSelect: false,
				mobileSelect: false,
				codeSelect: false,
				cardId: '',
				url: '',
				encryptedString: '',
			}
		},
		onLoad(options) {
		    this.cardId = options.cardId; 
			this.getCodeImg();
		},
		methods: {
			onSubmit(){
				let _this = this;
				if(this.name==''){
					uni.showToast({
					   title: '请填写姓名',
					   icon: 'none',
					   duration: 2000		
					});
					return false
				}
				if(this.name&&this.name.length>10){
					uni.showToast({
					   title: '姓名长度不能超过10位',
					   icon: 'none',
					   duration: 2000		
					});
					return false
				}
				if(this.mobile==''){
					uni.showToast({
					   title: '请填写手机号',
					   icon: 'none',
					   duration: 2000		
					});
					return false
				}
				let reg = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
				if(!reg.test(this.mobile)){
					uni.showToast({
					   title: '请修改手机号',
					   icon: 'none',
					   duration: 2000		
					});
					return false
				}
				if(this.code==''){
					uni.showToast({
					   title: '请填写验证码',
					   icon: 'none',
					   duration: 2000		
					});
					return false
				}
				uni.showModal({
					title: '提示',
					content: '是否立即机卡重绑？机卡重绑后请继续前往操作记录查看结果',
					success: function (res) {
						if (res.confirm) {
							_this.loading = true;
							_this.$req.userRecharge('card/unlock/imgcheck',{
								id: _this.cardId,				//cardId
								name: _this.name,				//姓名
								phone: _this.mobile,			//手机号
								imgCode: _this.code,			//验证码
								uid: _this.encryptedString,
							}).then(res=>{
								uni.showToast({
								   title: '请稍后到卡日志查看操作结果',
								   icon: 'none',
								   duration: 2000		
								});
							})
							_this.code = '';
							_this.encryptedString = '';
							_this.url = '';
							_this.getCodeImg()
							_this.loading = false;
						} else if (res.cancel) {
							uni.showToast({
							   title: '已取消机卡重绑操作',
							   icon: 'none',
							   duration: 2000		
							});	
						}
					}
				});		
				
			},
			//获取验证码
			getCodeImg() {
				this.$req.sendCheck('/code/send/pictureCode/uid', {}).then(res => {
					if (res._result) {
						this.encryptedString = res.data
						this.url = '/api/oceanside-send-and-check/code/send/pictureCode/' + res.data
					}
				})
			},
			getUserName(e){
				this.name = e.detail.value; 
				this.nameSelect = true;
				this.mobileSelect = false;
				this.codeSelect = false;
			},
			getUserMobile(e){
				this.mobile = e.detail.value; 
				this.nameSelect = false;
				this.mobileSelect = true;
				this.codeSelect = false;
			},
			getCode(e){
				this.code = e.detail.value;  
				this.nameSelect = false;
				this.mobileSelect = false;
				this.codeSelect = true;
			},
			changeBorder(obj){
				let arr = ['nameSelect','phoneSelect','codeSelect'];
				for(let item of arr){
					this[item] = false;
					if(item == obj){
						this[item] = true;
					}
				}
			}
		}
	}
</script>

<style>
	.bg {
		width: 100%;
		height: 100%;
		position: absolute;
		top: 0;
		left: 0;
		background-image: linear-gradient(to top, #fdfbfb 0%, white 100%);
	}

	.form {
		width: 90%;
		height: auto;
		position: relative;
		top: 0;
		left: 5%;
		padding: 20upx 0;
	}

	.label {
		width: 100%;
		height: 80upx;
		position: relative;
		line-height: 80upx;
		font-size: 28upx;
		color: #333333;
		margin-top: 10px;
	}

	.value {
		width: 100%;
		height: 80upx;
		position: relative;
		border-bottom: 1px solid #e6e6e6;
	}
	.code{
		width: 100%;
		height: 80upx;
		position: relative;
		border-bottom: 1px solid #e6e6e6;
		position: relative;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.uni-input {
		height: 50upx;
		padding: 7px 0;
		line-height: 25px;
		font-size: 30upx;
		background: #FFF;
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		flex: 1;
	}
	.bottom{
		width: 90%;
		height: auto;
		position: absolute;
		bottom: 0;
		left: 0;
		padding: 10% 5%;
	}
	.btn{
		width: 100%;
		height: 80upx;
		text-align: center;
		line-height: 80upx;
		font-size: 30upx;
		color: #FFFFFF;
		background-image: linear-gradient(to bottom, #A0C6FF 0%, #408CFF 100%);
		letter-spacing: 5px;
	}
</style>
